<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>类</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oop4.html">类与对象（PHP 4）</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="keyword.extends.html">继承</a></div>
 <div class="up"><a href="oop4.html">类与对象（PHP 4）</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="keyword.class" class="sect1">
  <h2 class="title"><em>类</em></h2>
  <p class="para">
   类是变量与作用于这些变量的函数的集合。变量通过<em>var</em>来定义，函数通过
   <em>function</em> 来定义，而类则通过下面的语法定义：
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$items</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;购物车中的物品<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;将&nbsp;$num&nbsp;个&nbsp;$artnr&nbsp;物品加入购物车<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">items</span><span style="color: #007700">[</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">]&nbsp;+=&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;将&nbsp;$num&nbsp;个&nbsp;$artnr&nbsp;物品从购物车中取出<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">remove_item</span><span style="color: #007700">(</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">items</span><span style="color: #007700">[</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">]&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">items</span><span style="color: #007700">[</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">]&nbsp;-=&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">items</span><span style="color: #007700">[</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">items</span><span style="color: #007700">[</span><span style="color: #0000BB">$artnr</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   上面的例子定义了一个 Cart
   类，这个类由购物车中的商品构成的数组和两个用于从购物车中添加和删除商品的函数组成。
  </p>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    <em class="emphasis">不能</em>将一个类的定义分割到多个文件中。也<em class="emphasis">不能</em>将一个类的定义分割到多个
    PHP 块中，除非该分割是在一个方法声明内部。以下用法将不起作用：
   </p>
   <p class="para">
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">test&nbsp;</span><span style="color: #007700">{<br /></span><span style="color: #0000BB">?&gt;<br />&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="simpara">
    但是以下用法是可以的：
   </p>
   <p class="para">
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">test&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>

  <p class="simpara">
   以下警告仅用于 PHP 4。
  </p>

  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    名称 <em>stdClass</em> 已经被 Zend 使用并保留。不能在 PHP
    代码中定义名为 <em>stdClass</em> 的类。
   </p>
  </div>

  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    函数名 <em>__sleep</em> 和
    <em>__wakeup</em> 在
    PHP 类中是魔术函数。除非想要与之联系的魔术功能，否则在任何类中都不能以此命名函数。
   </p>
  </div>

  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    PHP 将所有以 __ 开头的函数名保留为魔术函数。除非想要使用一些见于文档中的魔术功能，否则建议不要在
    PHP 中将函数名以 __ 开头。
   </p>
  </div>

  <p class="simpara">
   在 PHP 4 中，<em>var</em>
   变量的值只能初始化为常量。用非常量值初始化变量，需要一个初始化函数，该函数在对象被创建时自动被调用。这样一个函数被称之为构造函数（见下面）。
  </p>
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;PHP&nbsp;4&nbsp;中不能这样用&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$todays_date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$firstname</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$owner&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Fred&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'Jones'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;不过包含有常量的数组可以&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">var&nbsp;</span><span style="color: #0000BB">$items&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"VCR"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"TV"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;应该这样进行&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$todays_date</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$owner</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$items&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"VCR"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"TV"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Cart</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">todays_date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'firstname'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;etc.&nbsp;.&nbsp;.&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   类也是一种类型，就是说，它们是实际变量的蓝图。必须用
   <em>new</em> 运算符来创建相应类型的变量。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$cart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Cart</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$another_cart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Cart</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$another_cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #DD0000">"0815"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   上述代码创建了两个 Cart 类的对象 <var class="varname"><var class="varname">$cart</var></var> 和
   <var class="varname"><var class="varname">$another_cart</var></var>，对象 <var class="varname"><var class="varname">$cart</var></var> 的方法 add_item()
   被调用时，添加了 1 件 10 号商品。对于对象 <var class="varname"><var class="varname">$another_cart</var></var>，3
   件 0815 号商品被添加到购物车中。
  </p>

  <p class="para">
   <var class="varname"><var class="varname">$cart</var></var> 和 <var class="varname"><var class="varname">$another_cart</var></var> 都有方法
   add_item()，remove_item() 和一个 items
   变量。它们都是明显的函数和变量。可以把它们当作文件系统中的某些类似目录的东西来考虑。在文件系统中，可以拥有两个不同的
   <var class="filename">README.TXT</var> 文件，只要不在相同的目录中。正如从为了根目录访问每个文件需要输入该文件的完整的路径名一样，必须指定需要调用的函数的完整名称：在
   PHP 术语中，根目录将是全局名字空间，路径名符号将是
   <em>-&gt;</em>。因而，名称 <var class="varname"><var class="varname">$cart->items</var></var>
   和 <var class="varname"><var class="varname">$another_cart->items</var></var>
   命名了两个不同的变量。注意变量名为 <var class="varname"><var class="varname">$cart->items</var></var>，不是
   <var class="varname"><var class="varname">$cart->$items</var></var>，那是因为在 PHP 中一个变量名只有一个单独的美元符号。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;正确，只有一个&nbsp;$<br /></span><span style="color: #0000BB">$cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">items&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"10"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;不正确，因为&nbsp;$cart-&gt;$items&nbsp;变成了&nbsp;$cart-&gt;""<br /></span><span style="color: #0000BB">$cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$items&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"10"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;正确，但可能不是想要的结果：<br />//&nbsp;$cart-&gt;$myvar&nbsp;变成了&nbsp;$cart-&gt;items<br /></span><span style="color: #0000BB">$myvar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'items'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$myvar&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"10"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   在定义类的时候，无法得知将使什么名字的对象来访问：在编写 Cart
   类时，并不知道之后对象的名称将会命名为 <var class="varname"><var class="varname">$cart</var></var>
   或者 <var class="varname"><var class="varname">$another_cart</var></var>，还是什么其它的名字。因而你不能在类中使用
   <var class="varname"><var class="varname">$cart->items</var></var>。然而为了类定义的内部访问自身的函数和变量，可以使用伪变量
   <var class="varname"><var class="varname">$this</var></var>
   来达到这个目的。<var class="varname"><var class="varname">$this</var></var> 变量可以理解为“我自己的”或者“当前对象”。因而
   &#039;<var class="varname"><var class="varname">$this->items[$artnr]</var></var> += <var class="varname"><var class="varname">$num</var></var>&#039; 可以理解为“我自己的物品数组的
   <var class="varname"><var class="varname">$artnr</var></var> 计数器加
   <var class="varname"><var class="varname">$num</var></var>”或者“在当前对象的物品数组的
   <var class="varname"><var class="varname">$artnr</var></var> 计数器加 <var class="varname"><var class="varname">$num</var></var>”。
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    伪变量 <var class="varname"><var class="varname">$this</var></var>
    通常未定义，如果其所在的方法是被静态调用的话。但这不是个严格规定：如果一个方法被从另一个对象内静态调用的话，则
    <var class="varname"><var class="varname">$this</var></var> 会被定义。此时
    <var class="varname"><var class="varname">$this</var></var> 的值是那个发出调用的对象。用下例演示：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$this&nbsp;is&nbsp;defined&nbsp;('</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">")\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\$this&nbsp;is&nbsp;not&nbsp;defined.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">B<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <p class="para">以上例程会输出：</p>
     <div class="example-contents screen">
<div class="cdata"><pre>
$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.
</pre></div>
     </div>
    </div>
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    有一些不错的函数用来处理类和对象。应该关注一下<a href="ref.classobj.html" class="link">类／对象函数</a>。
   </p>
  </p></blockquote>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oop4.html">类与对象（PHP 4）</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="keyword.extends.html">继承</a></div>
 <div class="up"><a href="oop4.html">类与对象（PHP 4）</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
